/**
 * Malsha 2014-12-22
 */

DeliveryMethod = new Object();

DeliveryMethod.getFieldSet = function() {

	var grid = null;
	var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
		clicksToMoveEditor : 2,
		autoCancel : false
	});
	grid = Ext.create('Ext.grid.Panel', {
		layout : {
			type : 'fit'
		},
		store : CommenStore.delevaryMethodStore,
		height : 200,
		anchor : '100%',
		columns : [ {
			header : 'Code',
			dataIndex : 'code',
			width : 150,
			flex : 1
		}, {
			header : 'Description',
			dataIndex : 'description',
			width : 150,
			flex : 1,
			editor : {}
		}, {
			header : 'Status',
			xtype : 'booleancolumn',
			dataIndex : 'status',
			align : 'center',
			trueText : 'Active',
			falseText : 'Inactive',
			width : 100,
			flex : 1,
			editor : {
				xtype : 'checkboxfield',
				allowBlank : false
			}
		} ],
		plugins : [ rowEditing ],
		dockedItems : [ {
			xtype : 'toolbar',
			dock : 'top',
			items : [ '->', {
				xtype : 'button',
				text : 'Add',
				listeners : {
					click : function() {
						var description = Ext.getCmp('description').getValue();
						var code = Ext.getCmp('code').getValue();
						if (description == null || description == "") {
							Ext.Msg.alert('Error', "Description can not be empty");
						} else if (code == null || code == "") {
							Ext.Msg.alert('Error', "Code can not be empty");
						} else {
							var recordIndex = CommenStore.delevaryMethodStore.findBy(function(record, id) {
								if (record.get('description') === description || record.get('code') === code) {
									return true;
								}
								return false;
							});

							if (recordIndex != -1) {
								Ext.Msg.alert('Error', "Duplicate recode fund");
							} else {
								CommenStore.delevaryMethodStore.add({
									description : description,
									code : code,
									id : 0,
									status : true,
								});
								this.up('form').getForm().reset();
							}
						}
					}
				}
			} ]
		} ],
		listeners : {
			render : function(p) {
				CommenStore.delevaryMethodStore.load();
			}
		}
	});

	return Ext.create('Ext.form.FieldSet', {
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'textfield',
			id : 'code',
			name : 'code',
			anchor : '50%',
			fieldLabel : 'Code<span style="color:red">*</span>'
		}, {
			xtype : 'textarea',
			name : 'description',
			id : 'description',
			fieldLabel : 'Description<span style="color:red">*</span>'
		}, grid ]
	});

};

DeliveryMethod.getCenterPanel = function() {

	return formPanel = Ext.create('Ext.form.FormPanel', {
		title : 'Delivery Method',
		bodyPadding : '20',
		autoScroll : true,
		frame : true,
		defaults : {
			labelWidth : 175,
			anchor : '65%',
		},
		fieldDefaults : {
			labelAlign : 'left'
		},
		items : [ DeliveryMethod.getFieldSet() ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
				CommenStore.delevaryMethodStore.removeAll();
				CommenStore.delevaryMethodStore.load();
			}
		}, {
			text : 'Save',
			id : 'save',
			handler : function() {
				var form = this.up('form').getForm();
				if (form.isValid()) {
					formPanel.setLoading('Saving...');
					var deliverymethods = [];
					CommenStore.delevaryMethodStore.each(function(record) {
						var obj = {
							"id" : record.get('id'),
							"code" : record.get("code"),
							"description" : record.get("description"),
							"status" : record.get("status")
						};
						deliverymethods.push(obj);
					});
					form.url = 'addDeliveryMethods.action';
					form.submit({
						params : {
							"deliverymethods" : Ext.encode(deliverymethods),
						},
						success : function(form, action) {
							form.reset();
							Ext.getCmp('save').setText('Save');
							CommenStore.delevaryMethodStore.removeAll();
							Ext.Msg.alert('Success', 'Saved Successfully');
							CommenStore.delevaryMethodStore.load();
							formPanel.setLoading(false);
						},
						failure : function(form, action) {
							Ext.Msg.alert('Failed', 'There is some error returned from the server.');
							formPanel.setLoading(false);
						}
					});

				}
			}
		} ],
		listeners : {
			render : function(p) {
				if (App.screenPermiton.getAt(0).data.create == 0)
					Ext.getCmp('save').disable();
				else
					Ext.getCmp('save').enable();
			}
		}
	});

};